/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yxj.yunying;

import java.awt.Color;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class subjectInfo extends javax.swing.JFrame {

    /**
     * Creates new form subjectInfo
     */
    public subjectInfo() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        startTime = new javax.swing.JTextField();
        endTime = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        queryButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        sbtid = new javax.swing.JTextField();
        jProgressBar1 = new javax.swing.JProgressBar();
        waitLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 535, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 361, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("医生站运营工具");
        setIconImage(Toolkit.getDefaultToolkit().getImage("yxj.png") );
        setLocation(new java.awt.Point(400, 300));

        jLabel1.setText("起始时间：");

        jLabel2.setText("结束时间：");

        startTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startTimeActionPerformed(evt);
            }
        });

        endTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endTimeActionPerformed(evt);
            }
        });

        jLabel3.setText("（格式：2018-07-30）");

        queryButton.setText("查询");
        queryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                queryButtonActionPerformed(evt);
            }
        });

        clearButton.setText("清空");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "话题id", "pv", "uv", "收藏", "打赏数", "购买人数", "发言人数", "分享数", "标题"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel4.setText("话题id：");

        sbtid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sbtidActionPerformed(evt);
            }
        });

        waitLabel.setForeground(new java.awt.Color(255, 51, 51));

        jMenu1.setText("功能");

        jMenuItem1.setText("查询话题信息");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1052, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(startTime, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(47, 47, 47)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(endTime, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sbtid, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(queryButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(clearButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(waitLabel)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(startTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(endTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(queryButton)
                    .addComponent(clearButton)
                    .addComponent(jLabel4)
                    .addComponent(sbtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(waitLabel))
                .addGap(14, 14, 14)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        dtm.setRowCount(0);
        startTime.setText("");
        endTime.setText("");
        sbtid.setText("");
        jProgressBar1.setMaximum(0);
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setValue(0);
        queryButton.setEnabled(true);
        waitLabel.setOpaque(true);
        waitLabel.setForeground(Color.red);
        waitLabel.setText("");
    }//GEN-LAST:event_clearButtonActionPerformed

    private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_queryButtonActionPerformed
        //获取工具输入参数
        String start = startTime.getText();
        String end = endTime.getText();
        String subjecttid = sbtid.getText();
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        jProgressBar1.setMaximum(0);
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setValue(0);
        dtm.setRowCount(0);
        if (("").equals(start) && ("").equals(end) && ("").equals(subjecttid)) {
            waitLabel.setOpaque(true);
            waitLabel.setForeground(Color.red);
            waitLabel.setText("请输入参数");
        } else {
            queryButton.setEnabled(false);
            waitLabel.setOpaque(true);
            waitLabel.setForeground(Color.red);
            waitLabel.setText("正在查询，请耐心等待..");

            new Thread("first") {
                public void run() {
                    //往表格写数据

                    try {
                        Connection conn = null;
                        Class.forName("com.mysql.jdbc.Driver");
                        // String url = "jdbc:mysql://10.10.38.251:3306/db_yxj_studio";
                        String url = "jdbc:mysql://192.168.1.245:54673/db_yxj_studio";
                        conn = DriverManager.getConnection(url, "yxj", "yxj@2015");
                        Statement stmt = conn.createStatement();
                        String sql = "select sbtid from db_yxj_studio.t_subject where status=0";
                        if (!("").equals(start)) {
                            sql = sql + " and start_time>UNIX_TIMESTAMP('" + start + "') ";
                        }
                        if (!("").equals(end)) {
                            sql = sql + " and start_time<UNIX_TIMESTAMP('" + end + "') ";
                        }
                        if (!("").equals(subjecttid)) {
                            sql = sql + " and sbtid = '" + subjecttid + "'";
                        }

                        System.err.println("sql:" + sql);
                        ResultSet rs = stmt.executeQuery(sql);
                        List<String> list = new ArrayList();
                        // 展开结果集数据库
                        while (rs.next()) {
                            // 通过字段检索
                            String sbtid = rs.getString("sbtid");
                            list.add(sbtid);
                        }
                        System.err.println(list);
                        int rowCount = list.size();
                        int i = 1;
                        for (String sid : list) {

                            String sql2 = "select \n"
                                    + "(select sum(num) pv from db_yxj_studio.t_subject_record where sbtid='" + sid + "') pv,\n"
                                    + "(select count(1) uv from db_yxj_studio.t_subject_record where sbtid='" + sid + "') uv,\n"
                                    + "(select count(1) '收藏' from db_yxj_content.t_collect where cntid=300162419) sc,\n"
                                    + "(select count(1) '打赏数' from db_yxj_studio.t_subject_grant_order where status=0 and pay_status=1 and sbtid='" + sid + "') ds,\n"
                                    + "(select count(1) '购买' from db_yxj_studio.t_subject_enter_order where pay_status=1 and sbtid='" + sid + "') gm,\n"
                                    + "(select count(1) '发言人数' from db_yxj_studio.t_subject_msg_normal where status=0 and sbtid='" + sid + "') fy,\n"
                                    + "(select count(1) '分享数' from db_yxj_stat.t_stat_share where type_id ='" + sid + "') gx,\n"
                                    + "(select title from db_yxj_studio.t_subject where sbtid='" + sid + "') title \n"
                                    + "from dual";
                            ResultSet rs2 = stmt.executeQuery(sql2);
                            while (rs2.next()) {
                                String pv = rs2.getString("pv");
                                String uv = rs2.getString("uv");
                                String sc = rs2.getString("sc");
                                String ds = rs2.getString("ds");
                                String gm = rs2.getString("gm");
                                String fy = rs2.getString("fy");
                                String gx = rs2.getString("gx");
                                String title = rs2.getString("title");

                                Vector V_Add = new Vector();
                                V_Add.add(sid);
                                V_Add.add(pv);
                                V_Add.add(uv);
                                V_Add.add(sc);
                                V_Add.add(ds);
                                V_Add.add(gm);
                                V_Add.add(fy);
                                V_Add.add(gx);
                                V_Add.add(title);
                                dtm.addRow(V_Add);
                            }
                            jProgressBar1.setMaximum(rowCount);
                            jProgressBar1.setStringPainted(true);
                            jProgressBar1.setValue(i++);
                            if (jProgressBar1.getValue() == jProgressBar1.getMaximum()) {
                                queryButton.setEnabled(true);
                                waitLabel.setOpaque(true);
                                waitLabel.setForeground(Color.green);
                                waitLabel.setText("查询完毕");
                            }
                        }

                        conn.close();

                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(subjectInfo.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SQLException ex) {
                        Logger.getLogger(subjectInfo.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }.start();

        }
    }//GEN-LAST:event_queryButtonActionPerformed

    private void startTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startTimeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_startTimeActionPerformed

    private void endTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endTimeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_endTimeActionPerformed

    private void sbtidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sbtidActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sbtidActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(subjectInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(subjectInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(subjectInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(subjectInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new subjectInfo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JTextField endTime;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton queryButton;
    private javax.swing.JTextField sbtid;
    private javax.swing.JTextField startTime;
    private javax.swing.JLabel waitLabel;
    // End of variables declaration//GEN-END:variables
}
